﻿using MainScene;
using MainScene.Evt.Command;
using MainScene.Evt.Condition;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;

public class LevelMapSubEditor
{

    public static readonly GUILayoutOption ListCellWidth = GUILayout.Width(700);
    public static readonly GUILayoutOption ButtonWidth = GUILayout.Width(50);
    public static readonly GUILayoutOption PanelWidth = GUILayout.Width(400);
    public static readonly float PanelWidthFloat = 400;
    public static readonly float PanelHeightFloat = 300;
    public static readonly GUILayoutOption LableWidth = GUILayout.Width(100);
    public static readonly GUILayoutOption OptionHeight = GUILayout.Height(400);
    #region 数据创建
    public static CommandData CreateSceneEvtCommand(EvtCommandType type)
    {
        switch (type)
        {
            case EvtCommandType.SetUnitAutoMove:
                return new SetUnitAutoMoveData();
            case EvtCommandType.ActionCommand:
                return new ActionCommandData();
            case EvtCommandType.ActiveTrigger:
                return new ActiveTriggerCommandData();
            case EvtCommandType.ChangeToFixCamera:
                return new ChangeToFixCameraCommandData();
            case EvtCommandType.ChangeToFixTargetOffset:
                return new ChangeToFixTargetOffsetCommondData();
            case EvtCommandType.ChangeCameraTarget:
                return new ChangeCameraTargetCommandData();
            case EvtCommandType.ChangeUnitAI:
                return new ChangeUnitAICommandData();
            case EvtCommandType.SetFieldOfView:
                return new SetFieldOfViewCommandData();
            case EvtCommandType.SetObjectVisible:
                return new SetObjectVisibleCommandData();
            case EvtCommandType.SetOrientation:
                return new SetOrientationCommandData();
            case EvtCommandType.SetPosition:
                return new SetPositionCommandData();
            case EvtCommandType.SetUnitAIActive:
                return new SetUnitAIActiveCommandData();
            case EvtCommandType.ShowTalk:
                return new ShowTalkCommandData();
            case EvtCommandType.BeginOneFight:
                return new BeginOneFightData();
            case EvtCommandType.PlayEffect:
                return new SetEffectCommandData();
            case EvtCommandType.PlayPlot:
                return new PlayPlotCommandData();
            case EvtCommandType.DelateStory:
                return new DelateStoryCommandData();
            case EvtCommandType.SceneNPCBattleFight:
                return new SceneNPCBattleFightData();
            case EvtCommandType.HideLoadingPanel:
                return new HideLoadingPanelData();
            case EvtCommandType.SliderCell:
                return new SetSliderCommandData();
            case EvtCommandType.JumpCell:
                return new JumpCommandData();
            case EvtCommandType.RaidEndCell:
                return new RaidEndCellData();
            case EvtCommandType.PlaySceneEffect:
                return new PlaySceneEffectData();
            case EvtCommandType.AnimatorState:
                return new AnimatorStateData();
            case EvtCommandType.ChangeUICondtion:
                return new ChangeUIConditionLabData();
            case EvtCommandType.HideSceneGameObject:
                return new HideSenceGameObjectData();
            case EvtCommandType.RotateSceneObj:
                return new SetRotateSceneObjData();
            case EvtCommandType.PlayEffectBone:
                return new PlayEffectBoneData();
        }
        return null;
    }

    public static ConditionData CreateSceneEvtCondition(EvtConditionType type)
    {
        switch (type)
        {
            //case EvtConditionType.OnAwaysArea:
            //    return new OnAreaTriggerAwaysConditionData();
            case EvtConditionType.OnAction:
                return new OnActionConditionData();
            case EvtConditionType.OnArea:
                return new OnAreaTriggerConditionData();
            case EvtConditionType.OnEffect:
                return new OnEffectConditionData();
            case EvtConditionType.OnSceneEvt:
                return new OnSceneEventConditionData();
            case EvtConditionType.OnTriggerEvt:
                return new OnTriggerMsgConditionData();
            case EvtConditionType.OnUnitDead:
                return new OnUnitDeadConditonData();
            case EvtConditionType.OnHoldRole:
                return new OnHoldRoleConditionData();
            case EvtConditionType.OnCatchRole:
                return new OnCatchRoleConditionData();
            case EvtConditionType.OnGameTrigger:
                return new OnGameTriggerEventData();
            case EvtConditionType.OnUnitClick:
                return new OnUnitClickConditionData();
            case EvtConditionType.OnAreaMutil:
                return new OnAreaTriggerMutliConditionData();
            case EvtConditionType.OnBeScanning:
                return new OnUnitBeScanningData();
            case EvtConditionType.OnScanningNum:
                return new OnUnitScanningNumsData();
        }
        return null;
    }
    public static string DataToString(Type type,object obj)
    {
        FieldInfo[] fieldArr = type.GetFields();
        string field_name, field_value;
        string content = type.ToString();
        content = content.Substring(content.LastIndexOf('.')+1,11)+"...";
        for (int i = 0; i < fieldArr.Length; i++)
        {
            field_name = fieldArr[i].Name;
            field_value = fieldArr[i].GetValue(obj).ToString();
            content = string.Format("{0} {1} : {2}", content, field_name, field_value);
        }
        return content;
    }
    #endregion

    #region 编辑属性
    public static object EditorData(Type type, object obj, GUILayoutOption cell_width, GUILayoutOption lable_width)
    {
        DrawObjectData(obj);
        return obj;
    }

    private static int int_val = 0;
    private static float float_val = 0f;
    private static bool bool_val = false;
    private static string string_val = string.Empty;
    private static object obj_val = null;
    private static int removeListIndex = -1;

    private static void DrawObjectData(object obj)
    {
        FieldInfo fieldInfo;
        FieldInfo[] fieldArr = obj.GetType().GetFields();
        for (int index = 0; index < fieldArr.Length; ++index)
        {
            fieldInfo = fieldArr[index];
            if (fieldInfo.FieldType == typeof(int))
            {
                int_val = EditorGUILayout.IntField(fieldInfo.Name, (int)fieldInfo.GetValue(obj));
                fieldInfo.SetValue(obj, int_val);
            }
            else if (fieldInfo.FieldType == typeof(float))
            {
                float_val = EditorGUILayout.FloatField(fieldInfo.Name, (float)fieldInfo.GetValue(obj));
                fieldInfo.SetValue(obj, float_val);
            }
            else if (fieldInfo.FieldType == typeof(string))
            {
                string_val = EditorGUILayout.TextField(fieldInfo.Name, (string)fieldInfo.GetValue(obj));
                fieldInfo.SetValue(obj, string_val);
            }
            else if (fieldInfo.FieldType == typeof(bool))
            {
                bool_val = EditorGUILayout.Toggle(fieldInfo.Name, (bool)fieldInfo.GetValue(obj));
                fieldInfo.SetValue(obj, bool_val);
            }
            else if (fieldInfo.FieldType.IsEnum)
            {
                obj_val = Enum.Parse(fieldInfo.FieldType, fieldInfo.GetValue(obj).ToString());
                obj_val = EditorGUILayout.EnumPopup(fieldInfo.Name, (Enum)obj_val);
                fieldInfo.SetValue(obj, obj_val);
            }
            else if (fieldInfo.FieldType == typeof(List<string>))
            {
                removeListIndex = -1;
                var values = fieldInfo.GetValue(obj) as List<string>;
                EditorGUILayout.BeginVertical("Box");
                //List按钮
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("列表", GUILayout.Width(80));
                if (GUILayout.Button("AddNew"))
                {
                    values.Add("");
                }
                EditorGUILayout.EndHorizontal();
                if (values != null && values.Count > 0)
                {
                    for (int i = 0; i < values.Count; i++)
                    {
                        EditorGUILayout.BeginHorizontal();
                        values[i] = EditorGUILayout.TextField(values[i]);
                        if (GUILayout.Button("delate"))
                        {
                            removeListIndex = i;
                        }
                        EditorGUILayout.EndHorizontal();
                    }
                    if (removeListIndex >= 0)
                    {
                        values.RemoveAt(removeListIndex);
                    }
                }

                EditorGUILayout.EndVertical();
                fieldInfo.SetValue(obj, values);
            }
            else if (fieldInfo.FieldType == typeof(Vector3Ex))
            {
                var value = (Vector3Ex)fieldInfo.GetValue(obj);
                value.UpdateValue(EditorGUILayout.Vector3Field("Euler", value.GetVec3()));
                fieldInfo.SetValue(obj, value);
            }
            else
            {
                EditorGUILayout.LabelField(fieldInfo.Name, obj != null ? fieldInfo.GetValue(obj).ToString() : string.Empty);
            }
        }
    }
    #endregion
}
